package org.android.kumoh_ing;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.StringTokenizer;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.Toast;

import com.facebook.android.FacebookError;
import com.facebook.android.Util;

public class GroupTextActivity extends Activity{
	FeedListView feedList;
    FeedListAdapter2 feedAdapter;
    static String group_id;
    CheckBox checkbox;
    private String curLocation = "";
    private LocationDBTransfer locDBTrans ;
//    private String AllMessage = "";
    static String s_id = "";
    static String s_name = "";
    static String s_date = "";
    static String s_msg = "";
  static String s_add = "";
    
    private ArrayList<String> list_name = new ArrayList<String>();
    private ArrayList<String> list_date = new ArrayList<String>();
    private ArrayList<String> list_msg = new ArrayList<String>();
    private ArrayList<String> list_id = new ArrayList<String>();
    private ArrayList<String> list_add = new ArrayList<String>();
    
//    public static final String cotext = "#%UserInfor";
	 public void onCreate(Bundle savedInstanceState) {
	       super.onCreate(savedInstanceState);
	       setContentView(R.layout.grouptext);
	      
	       group_id = getIntent().getExtras().getString("group_id");
	       locDBTrans = new LocationDBTransfer();
	      
	       feedList = (FeedListView) findViewById(R.id.feedList);
	        feedAdapter = new FeedListAdapter2(this);
	        feedList.setAdapter(feedAdapter);
	        
	        feedList.setOnDataSelectionListener(new OnDataSelectionListener() {
				public void onDataSelected(AdapterView parent, View v, int position, long id) {
					FeedItem curItem = (FeedItem) feedAdapter.getItem(position);
					String curText = curItem.getMessage();
					
//					StringTokenizer st = new StringTokenizer(AllMessage, "&&"); 
//					while (st.hasMoreTokens()) { 
//						String mes = st.nextToken();
//						
//						if(mes.length()>=15 && mes.substring(0, 15).equalsIgnoreCase("#%UserLocation=")==true){
//							mes = mes.replaceAll("#%UserLocation=",""); 
//							StringTokenizer dt = new StringTokenizer(mes, "//");
//						
//						
//								curLocation = dt.nextToken();
//								String curMessage = dt.nextToken();
//							
//								if(curMessage.equalsIgnoreCase(curText))
//								{
									s_id = list_id.get(position);
									s_name = list_name.get(position);
									s_date = list_date.get(position);
									s_msg = list_msg.get(position);
									s_add = list_add.get(position);
									Intent intent = new Intent(getBaseContext(),GtestActivity.class);
									
									
									startActivity(intent);
//									break;
//							
//								}
//				
//						}
//						if((st.hasMoreTokens()== false))
//						{
//							Toast.makeText(getApplicationContext(), "Not Found Location", 1000).show();
//							
//						}
//					}
					
					
				}
			});
	        getUserTimeline();
	        
	        
	        
	 }
	 private void getUserTimeline() {
			try {
				list_id.clear();
				list_name.clear();
				list_date.clear();
				list_msg.clear();
				list_add.clear();
								
				String responseStr = facebookvalue.FacebookInstance.request(group_id+"/feed");
				
				JSONObject resultObj = Util.parseJson(responseStr);
				JSONArray jArray = resultObj.getJSONArray("data");
				
				
				for(int i = 0; i < jArray.length(); i++) {
					/////////////////////////////////////////////////////////////
					JSONObject obj = jArray.getJSONObject(i);
					
					String curMessage = "";
					String curAddress = "";
					
						
					///////////////////////////////////////////////////////////////
						
	
						String curId = obj.getString("id");
						JSONObject fromObj = obj.getJSONObject("from");
					
						String curName = fromObj.getString("name");
						
						try {
							curMessage = obj.getString("message");
							curLocation = locDBTrans.getFeedLocation(curId);
							
							StringTokenizer st = new StringTokenizer(curLocation, " ");
							String lat = st.nextToken();
							String lon = st.nextToken();
							curAddress = getKoreadAddress(lat,lon);
							 
//							if(curMessage.length()>=12 && curMessage.substring(0, 12).equalsIgnoreCase("#%UserInfor=")==true){
//								continue;
//							}
							
//							AllMessage = AllMessage + curMessage + "&&";
//							curMessage = curMessage.replaceAll("#%UserLocation=",""); 
//							StringTokenizer st = new StringTokenizer(curMessage, "//");
//							while (st.hasMoreTokens()) { // 3
//
//								curLocation = st.nextToken();
//								StringTokenizer st1 = new StringTokenizer(curLocation, "$");
//								String lat = st1.nextToken();
//								String lon = st1.nextToken();
//								curAddress = getKoreadAddress(lat,lon);
//								curMessage = st.nextToken(); 
//							
//							}
						
							
						} catch(Exception ex) {
							ex.printStackTrace();
						}
	
						String dateStr = "";
						try {
							String curDate = obj.getString("created_time");
							Date curDateObj = facebookvalue.OrigDateFormat.parse(curDate);
							dateStr = facebookvalue.DateFormat.format(curDateObj);
							
						} catch(Exception ex) {
							ex.printStackTrace();
						}
						
						
						String curPicture = null;
		
						FeedItem curItem = new FeedItem(curName, dateStr, curMessage, curPicture, curAddress);
						feedAdapter.addItem(curItem);
						list_id.add(curId);
						list_name.add(curName);
						list_date.add(dateStr);
						list_msg.add(curMessage);
						list_add.add(curAddress);
					}
						
				

				feedAdapter.notifyDataSetChanged();

			} catch(Exception ex) {
				ex.printStackTrace();
			} catch(FacebookError ext) {
				ext.printStackTrace();
			}
			
			
		}
	 public String getKoreadAddress(String latPoint, String lngPoint){
		 StringBuilder sb = new StringBuilder();
		 Geocoder geoCoder = new Geocoder(this);
		 double lat=Double.parseDouble(latPoint);
		 double lng=Double.parseDouble(lngPoint);
		 
		 try{
			 List<Address> addresses;
			 addresses = geoCoder.getFromLocation(lat,lng,1);
			 if(addresses !=null && addresses.size()>0){
				 Address address =addresses.get(0);
			//	 sb.append(address.getCountryName()).append(" ");
				 sb.append(address.getLocality()).append(" ");
				 sb.append(address.getThoroughfare()).append(" ");
			//	 sb.append(address.getFeatureName()).append(" ");
			 }

		 }catch(Exception e){
			 e.printStackTrace();
		 }
		 
		 return sb.toString();
	 }
	 
	
}
